/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.test;

import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.isfd.constant.IsfdMeshModelConstants;
import com.comac.ins.isfd.domain.IsfdMeshModelBdfParseInfo;
import com.comac.ins.isfd.service.IIsfdMeshModelParseService;
import com.comac.ins.system.service.ISysDictDataService;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;

import java.util.List;


@SpringBootTest
public class IsfdMeshModelBDFCardCountTest {
    @Autowired
    private IIsfdMeshModelParseService meshModelParseService;

    @Autowired
    private ISysDictDataService iSysDictDataService;

    @BeforeAll
    static void setup() {
        System.out.println("初始化测试环境...");
    }

    @Test
    @Order(1)
    public void countBDFCards() {
        Long count = meshModelParseService.countBDFCards();
        System.out.println("卡片数量：" + count);
    }

    @Test
    @Order(2)
    public void createImportRecord() {
        // 查询parse表
        List<IsfdMeshModelBdfParseInfo> parseInfoList =  meshModelParseService.queryParseInfoList(null, 2);

        for (IsfdMeshModelBdfParseInfo parseInfo : parseInfoList) {
            meshModelParseService.createImportRecord(parseInfo, LoginHelper.getLoginUser());
        }

    }

    @Test
    @Order(3)
    public void checkDict(){
        boolean executeJob = Boolean.parseBoolean(iSysDictDataService.selectDictValueByTypeAndLabel(IsfdMeshModelConstants.MODEL_MESH, IsfdMeshModelConstants.MESH_JOB_STRUCTURED_SWITCH));
        System.out.println(executeJob);
    }

    @Test
    @Order(4)
    public void parseAndCreateImportRecord() {
        List<Document> documents = meshModelParseService.queryList(null);
        if (CollectionUtils.isEmpty(documents)) {
            throw new BaseException("没有BDF文件，任务调度完成");
        }

        List<String> ids = documents.stream()
            .map(document -> {
                Object idObj = document.get("_id");
                if (idObj instanceof ObjectId) {
                    return ((ObjectId) idObj).toHexString();
                }
                return idObj.toString();
            })
            .toList();

        meshModelParseService.executeParse(ids, LoginHelper.getLoginUser());
    }

    @AfterAll
    static void cleanup() {
        System.out.println("清理测试环境...");
    }
}
